package com.heima.admin.v1.controller;

import com.heima.admin.dto.ChannelPageDTO;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "频道管理", description = "频道管理API")
@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {

    @Autowired
    AdChannelService channelService;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据名称分页查询频道列表")
    @PostMapping("/list")
    public PageResult<AdChannel> list(@RequestBody ChannelPageDTO dto) {
        return channelService.pageQuery(dto);
    }

    @GetMapping("/channels")
    public ResponseResult<List<AdChannel>> listAll(){
        List<AdChannel> list = channelService.list();
        return ResponseResult.ok(list);
    }
    /**
     * 保存频道信息
     *
     * @param adChannel
     * @return
     */
    @ApiOperation("保存频道信息")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody AdChannel adChannel) {
        //非空判断
        if (adChannel == null || StringUtils.isEmpty(adChannel.getName())) {
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        boolean b = channelService.save(adChannel);
        if (!b) {
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }

    /**
     * 修改频道
     *
     * @param adChannel
     * @return
     */
    @ApiOperation("修改频道")
    @PutMapping("/update")
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        //非空判断
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdChannel channel = channelService.getById(adChannel.getId());
        if (StringUtils.isEmpty(channel)){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean b = channelService.updateById(adChannel);
        if (!b) {
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation("根据id删除")
    @DeleteMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable("id") Long id){
        AdChannel channel = channelService.getById(id);
        if (StringUtils.isEmpty(channel)){
            return ResponseResult.error(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (channel.getStatus()){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean b = channelService.removeById(id);
        if (!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }

}
